package com.bawei.bydayzg_two.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.base.mvvm.BaseFragment
import com.bawei.base.mvvm.BaseViewFragment
import com.bawei.bydayzg_two.R
import com.bawei.bydayzg_two.adapter.GoodsAdapter
import com.bawei.bydayzg_two.adapter.ONeAdapter
import com.bawei.bydayzg_two.adapter.TwoAdapter
import com.bawei.bydayzg_two.databinding.FragmentCapegoryBinding
import com.bawei.bydayzg_two.viewModel.CartGroyViewModel
import com.blankj.utilcode.util.ToastUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CapegoryFragment : BaseFragment<FragmentCapegoryBinding,CartGroyViewModel>() {
    lateinit var oNeAdapter: ONeAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter
    //
    var pageNum = 1
    var pageType = 0
    //
    override fun getLoutyId(): Int {
        return R.layout.fragment_capegory
    }

    override fun getViewModel(): Class<CartGroyViewModel> {
        return CartGroyViewModel::class.java
    }

    override fun initView() {

        //
        oNeAdapter = ONeAdapter()
        binding.rvOne.adapter = oNeAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(requireActivity())
        //
        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(requireActivity(),4)
        //
        goodsAdapter= GoodsAdapter()
        binding.rvThree.adapter = goodsAdapter
        binding.rvThree.layoutManager = GridLayoutManager(requireActivity(),2)

        //请求一次分类和一级分类的数据
        viewModel.cartGroy()

        //三级联动
        oNeAdapter.setOnItemClickListener { adapter, view, position ->
            //选中的发生变化
            oNeAdapter.getIndex(oNeAdapter.data[position].id)
            //数据发生变化
            twoAdapter.data.clear()//清空加载一级的数据
            twoAdapter.data.addAll(oNeAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            //二级默认选中的也发生了变化
            twoAdapter.getIndex(twoAdapter.data[0].id)
            //再次调用三级接口使其发生变化
            viewModel.goods(twoAdapter.data[0].id,"",2,false,pageNum,"","")
        }
        //点击二级分类，选中发生变化，商品列表变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            twoAdapter.getIndex(twoAdapter.data[position].id)
            //
            pageType = twoAdapter.data[position].id
            //
            viewModel.goods(twoAdapter.data[position].id,"",2,false,pageNum,"","")
            //
        }
        //
        binding.sm.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                //
                viewModel.goods(pageType,"",2,false,pageNum,"","")
                //
                binding.sm.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum ++
                //
                viewModel.goods(pageType,"",2,false,pageNum,"","")
                //
                binding.sm.finishLoadMore()
            }
        })

    }

    override fun initDate() {

        //
        viewModel.cartGroySuccess.observe(this){
            if (it.code == 200){
                oNeAdapter.data.clear()
                oNeAdapter.data.addAll(it.data)
                oNeAdapter.notifyDataSetChanged()
                //二级分类数据
                twoAdapter.data.clear()
                twoAdapter.data.addAll(it.data[0].child)
                //请求三级数据
                viewModel.goods(twoAdapter.data[0].id,"",2,false,1,"","")
            }
            //
            ToastUtils.showLong(it.message)
        }
        //
        viewModel.goodsSuccess.observe(this){
            if (it.code == 200){
                if (pageNum == 1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
            //
            ToastUtils.showLong(it.message)
        }
    }

}